/**
 * 
 */
package demo.pivot.base;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;

/**
 * @author Administrator
 * 
 */
public abstract class BaseView implements Application {

	protected Window window = null;

	/**
	 * 视图处理
	 * 
	 * @param display
	 *            org.apache.pivot.wtk.Display
	 * @param properties
	 *            org.apache.pivot.collections.Map<String, String>
	 * @throws Exception
	 */
	protected abstract void process(Display display, Map<String, String> properties) throws Exception;

	/**
	 * 返回一个 winodw 实例（在同一个 View 下为单例）
	 * 
	 * @return a window instance (Single Window)
	 */
	protected Window getWindowInstance() {
		if (window == null) window = new Window();
		return window;
	}

	/**
	 * Called when the application is starting up.
	 * 
	 * @param display
	 *            org.apache.pivot.wtk.Display
	 * @param properties
	 *            org.apache.pivot.collections.Map<String, String>
	 * @throws Exception
	 */
	@Override
	public void startup(Display display, Map<String, String> properties) throws Exception {
		window = getWindowInstance();
		process(display, properties);
	}

	/**
	 * Called when the application is being shut down. <br />
	 * 
	 * @param optional
	 *            If true, the shutdown may be cancelled by returning a value of true.
	 * @return true to cancel shutdown, false to continue.
	 * @throws Exception
	 */
	@Override
	public boolean shutdown(boolean optional) throws Exception {
		if (window != null) window.close();
		return false;
	}

	/**
	 * Called to notify the application that it is being suspended.
	 * 
	 * @throws Exception
	 */
	@Override
	public void suspend() throws Exception {}

	/**
	 * Called when a suspended application has been resumed.
	 * 
	 * @throws Exception
	 */
	@Override
	public void resume() throws Exception {}

}
